home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / advanced97 / multialphablend.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  3.9 KB  |  203 lines

  1. #include <assert.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <GL/glut.h>
  6.  
  7. GLUquadricObj *cone, *base, *qsphere;
  8.  
  9. #ifndef __sgi
  10. #define trunc(x) ((double)((int)(x)))
  11. #endif
  12.  
  13. void init(void)
  14. {
  15.   static GLfloat lightpos[] = {.5, .75, 1.5, 1};
  16.  
  17.   glEnable(GL_DEPTH_TEST); 
  18.   glEnable(GL_LIGHTING);
  19.   glEnable(GL_LIGHT0);
  20.  
  21.   glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
  22.  
  23.   cone = gluNewQuadric();
  24.   base = gluNewQuadric();
  25.   qsphere = gluNewQuadric();
  26.  
  27.   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  28. }
  29.  
  30. void reshape(GLsizei w, GLsizei h) 
  31. {
  32.   glViewport(0, 0, w, h);
  33.   
  34.   glMatrixMode(GL_PROJECTION);
  35.   glLoadIdentity();
  36.   gluPerspective(60, 1, .01, 10);
  37.   gluLookAt(0, 0, 2.577, 0, 0, -5, 0, 1, 0);
  38.   
  39.   glMatrixMode(GL_MODELVIEW);
  40.   glLoadIdentity();
  41. }
  42.  
  43. void draw_room(void)
  44. {
  45.   /* material for the walls, floor, ceiling */
  46.   static GLfloat wall_mat[] = {1.f, 1.f, 1.f, 1.f};
  47.  
  48.   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, wall_mat);
  49.  
  50.   glBegin(GL_QUADS);
  51.   
  52.   /* floor */
  53.   glNormal3f(0, 1, 0);
  54.   glVertex3f(-1, -1, 1);
  55.   glVertex3f(1, -1, 1);
  56.   glVertex3f(1, -1, -1);
  57.   glVertex3f(-1, -1, -1);
  58.  
  59.   /* ceiling */
  60.   glNormal3f(0, -1, 0);
  61.   glVertex3f(-1, 1, -1);
  62.   glVertex3f(1, 1, -1);
  63.   glVertex3f(1, 1, 1);
  64.   glVertex3f(-1, 1, 1);  
  65.  
  66.   /* left wall */
  67.   glNormal3f(1, 0, 0);
  68.   glVertex3f(-1, -1, -1);
  69.   glVertex3f(-1, -1, 1);
  70.   glVertex3f(-1, 1, 1);
  71.   glVertex3f(-1, 1, -1);
  72.  
  73.   /* right wall */
  74.   glNormal3f(-1, 0, 0);
  75.   glVertex3f(1, 1, -1);
  76.   glVertex3f(1, 1, 1);
  77.   glVertex3f(1, -1, 1);
  78.   glVertex3f(1, -1, -1);
  79.  
  80.   /* far wall */
  81.   glNormal3f(0, 0, 1);
  82.   glVertex3f(-1, -1, -1);
  83.   glVertex3f(1, -1, -1);
  84.   glVertex3f(1, 1, -1);
  85.   glVertex3f(-1, 1, -1);
  86.  
  87.   glEnd();
  88. }
  89.  
  90. void draw_cone(void)
  91. {
  92.   static GLfloat cone_mat[] = {0.f, .5f, 1.f, .5f};
  93.  
  94.   glPushMatrix();
  95.   glTranslatef(0, -1, 0);
  96.   glRotatef(-90, 1, 0, 0);
  97.  
  98.   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cone_mat);
  99.  
  100.   /* base is coplanar with floor, so turn off depth testing */
  101.   glDisable(GL_DEPTH_TEST);
  102.   gluDisk(base, 0., .3, 20, 1); 
  103.   glEnable(GL_DEPTH_TEST);
  104.  
  105.   gluCylinder(cone, .3, 0, 1.25, 20, 1);
  106.  
  107.   glPopMatrix();
  108. }
  109.  
  110. void draw_sphere(GLdouble angle)
  111. {
  112.   static GLfloat sphere_mat[] = {1.f, .5f, 0.f, .5f};
  113.  
  114.   glPushMatrix();
  115.   glTranslatef(0, -.3, 0);
  116.   glRotatef(angle, 0, 1, 0);
  117.   glTranslatef(.6, 0, 0);
  118.  
  119.   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, sphere_mat);
  120.   gluSphere(qsphere, .3, 20, 20);
  121.  
  122.   glPopMatrix();
  123. }
  124.  
  125. GLdouble get_secs(void)
  126. {
  127.     return glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  128. }
  129.  
  130. void draw(void)
  131. {
  132.     GLenum err;
  133.     GLdouble secs, degrees;
  134.  
  135.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  136.  
  137.     /* one revolution every 10 seconds... */
  138.     secs = get_secs();
  139.     secs = secs - 10.*trunc(secs / 10.);
  140.     degrees = (secs/10.) * (360.);
  141.  
  142.     draw_room();
  143.  
  144.     glEnable(GL_BLEND);
  145.     glEnable(GL_CULL_FACE);
  146.     if (degrees < 180) {
  147.       /* sphere behind cone */
  148.       glCullFace(GL_FRONT);
  149.       draw_sphere(degrees);
  150.       draw_cone();
  151.       glCullFace(GL_BACK);
  152.       draw_sphere(degrees);
  153.       draw_cone();
  154.     } else {
  155.       /* cone behind sphere */
  156.       glCullFace(GL_FRONT);
  157.       draw_cone();
  158.       draw_sphere(degrees);
  159.       glCullFace(GL_BACK);
  160.       draw_cone();
  161.       draw_sphere(degrees);
  162.     }
  163.     glDisable(GL_CULL_FACE);
  164.     glDisable(GL_BLEND);
  165.  
  166.     err = glGetError();
  167.     if (err != GL_NO_ERROR) printf("Error:  %s\n", gluErrorString(err));
  168.  
  169.     glutSwapBuffers();
  170. }
  171.  
  172. /* ARGSUSED1 */
  173. void key(unsigned char key, int x, int y)
  174. {
  175.   static int idle = 1;
  176.   if (key == 27) exit(0);
  177.   idle = (idle == 0);
  178.   if (idle) {
  179.     glutIdleFunc(draw);
  180.   } else {
  181.     glutIdleFunc(0);
  182.   }
  183.  
  184. }
  185.  
  186. main(int argc, char *argv[])
  187. {
  188.     glutInit(&argc, argv);
  189.     glutInitWindowSize(256, 256);
  190.     glutInitWindowPosition(0, 0);
  191.     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  192.     glutCreateWindow(argv[0]);
  193.     glutDisplayFunc(draw);
  194.     glutIdleFunc(draw);
  195.     glutKeyboardFunc(key);
  196.     glutReshapeFunc(reshape);
  197.     init();
  198.  
  199.     glutMainLoop();
  200.     return 0;
  201. }
  202.  
  203.